package models

import (
	"github.com/astaxie/beego/orm"
	"strconv"
)

type RunTimeModel struct {
	Id             int64
	Taskid         int64
	Call_phone     string
	Call_state     int
	Gateway_id     int64
	Call_time      string
	Answer_time    string
	Talk_crycle    int
	Call_id        string
	Tpl_id         int64
	Display_number string
	Number_map_id  int64
	Mute           string
	Mismatching    string
	Mismatch_times int
	Mute_times     int
}

func (a *RunTimeModel) TableName() string {
	return TableName("runtime")
}

func RunTimeAdd(p *RunTimeModel) (int64, error) {
	return orm.NewOrm().Insert(p)
}

//获取列表
func GetRunTimeInfo(owner int) (int, error) {

	var authStr = " "
	if owner != 1 {
		authStr = " and t.owner_id=" + strconv.Itoa(owner)
	}

	runTime := 0
	strSql := "SELECT count(1) " +
		"FROM ai_runtime r,ai_tpl t " +
		"WHERE r.tpl_id=t.id " + authStr + ";"

	err := orm.NewOrm().Raw(strSql).QueryRow(&runTime)

	if err != nil {
		ErrorLog(err)
		return runTime, err
	}

	return runTime, nil
}
//获取列表
func GetRunTimeList() ([]RunTimeModel, error) {

	arr := make([]RunTimeModel, 0)
	_, err := orm.NewOrm().QueryTable(TableName("runtime")).All(&arr)

	if err != nil {
		ErrorLog(err)
		return nil,err
	}
	return arr,nil
}
